package com.ctw.mall.controller;

import com.ctw.mall.consts.MallConst;
import com.ctw.mall.form.ShippingForm;
import com.ctw.mall.pojo.User;
import com.ctw.mall.service.IShippingService;
import com.ctw.mall.vo.ResponseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
public class ShippingController {

    @Autowired
    private IShippingService shippingService;

    //添加地址
    @PostMapping("/shipping")
    public ResponseVo add(@Valid @RequestBody ShippingForm shippingForm
                                    , HttpSession session){
        //涉及表单时，必传字段，记得进行表单验证
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return shippingService.add(user.getId(),shippingForm);
    }


    @DeleteMapping("/shippings/{shippingId}")
    public ResponseVo delete(@PathVariable Integer shippingId
                                , HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
      return   shippingService.delete(user.getId(),shippingId);
    }

    @PutMapping("/shippings/{shippingId}")
    public ResponseVo update(@Valid @RequestBody ShippingForm shippingForm
                               ,@PathVariable Integer shippingId
                                ,HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
      return  shippingService.update(user.getId(),shippingId,shippingForm);

    }

    //地址列表
    @GetMapping("/shippings")
    public ResponseVo<PageInfo> list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false,defaultValue = "1") Integer pageSize,
                                     HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
      return shippingService.list(user.getId(),pageNum,pageSize);
    }

}
